package com.vcc.web.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.io.FileUtil;
import com.vcc.core.annotations.KycReject;
import com.vcc.core.annotations.ROReject;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.ProgramException;
import com.vcc.core.util.IdWorker;
import com.vcc.core.util.RegexUtil;
import com.vcc.web.core.component.OssComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;

@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private OssComponent ossComponent;

    @RestPack
    @ROReject
    @PostMapping("upload")
    @SaCheckLogin
    public UploadVo upload(@RequestBody MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
//        String fileType = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        File targetFile = transferToFile(file);
        String fileType = FileUtil.getType(targetFile);
        System.out.println(fileType);
        if (!RegexUtil.checkStringIsNull(fileType) && !"jpg".equals(fileType) && !"png".equals(fileType) && !"jpeg".equals(fileType) && !"pdf".equals(fileType)) {
            throw new ProgramException("文件格式错误,仅支持jpg,png,jpeg,pdf格式");
        }
        String filename;
        if ("jpg".equals(fileType) || "png".equals(fileType) || "jpeg".equals(fileType)) {
            filename = IdWorker.getStringOrderId() + ".jpg";
        } else {
            filename = IdWorker.getStringOrderId() + ".pdf";
        }
        ossComponent.uploadFile(targetFile, filename);
        URL tokenUrl = ossComponent.generateTokenUrl(filename);
        return new UploadVo(filename, tokenUrl.toString());
    }

    public File transferToFile(MultipartFile multipartFile) {
        //选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            if (RegexUtil.checkStringIsNull(originalFilename)) {
                throw new ProgramException("文件名为空");
            }
            String[] filename = originalFilename.split("\\.");
            file = File.createTempFile(filename[0], filename[1]);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IOException e) {
            throw new ProgramException("文件转换失败");
        }
        return file;
    }

    @RestPack
    @ROReject
    @KycReject
    @GetMapping("remove")
    public Boolean remove(String fileName) {
        return ossComponent.remove(fileName);
    }

    @RestPack
    @GetMapping("generateTokenUrl")
    @SaCheckLogin
    public URL generateTokenUrl(String fileName) {
        return ossComponent.generateTokenUrl(fileName);
    }

    class UploadVo {
        private String name;
        private String url;

        public UploadVo() {
        }

        public UploadVo(String name, String url) {
            this.name = name;
            this.url = url;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }

}
